package com.CampusLife.controller;


import  com.CampusLife.bean.Login;
import  com.CampusLife.bean.User;
import com.alibaba.fastjson.JSONObject;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import  com.CampusLife.service.ILoginService;
import  com.CampusLife.service.IUerService;
import  com.CampusLife.until.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.bind.JAXBException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/login")
public class LoginController {
    @Resource
    private  IUerService iUerService;
    @Resource
    private ILoginService iLoginService;

    /**
     *
      进入登录页面
     * @return
     */
    @RequestMapping("/in")
    public String index(){
        return "/home/login";
    }
    //用户验证登录
    @RequestMapping("/login_verify")
    @ResponseBody
    public  int login_verify(String username,String password,HttpSession httpSession){
          //给页面传过来的密码加密
        String ps=Encrypt.MD5(password)+"wun";
          //再次加密防止密码外泄
        String  ps1=Encrypt.MD5(ps);
          User user=iUerService.login( username, ps1);
          //如果查询结果不为空，则证明该用户合法，否则该用户不合法
        if (user!=null){
            //将该用户的id保存到session
           httpSession.setAttribute("id",user.getId());
           httpSession.setAttribute("password",ps1);
           httpSession.setAttribute("username",user.getUsername());
            httpSession.setAttribute("userPhone",user.getPhone());
            // return "redirect:/login/record";
              return 0;
          }else {
             // return "home/login";
            return 1;
          }
    }
    //用户登录记录
    @RequestMapping("/record")
    public String record(HttpSession httpSession,Model model){
        DataTime dataTime=new DataTime();
        Login login=new Login();
        //获取当前登录用户名
        String name=(String)httpSession.getAttribute("username");
        System.out.println(name);
        model.addAttribute("name",name);
        login.setName(name);
        //调用datatime类中的方法，将当前时间转化成固定格式
        String data=dataTime.gettime();
        System.out.println(data);
        login.setTime(data);
        //调用方法
        iLoginService.add(login);
          return "redirect:/index/ToIndex";
    }



    //退出登录

    @RequestMapping("/login_out")
    public String  login_out(HttpSession httpSession){
       httpSession.removeAttribute("id");
        return "home/login";
    }

    //用户通过手机验证找回密码
    //发送验证码

    @RequestMapping(value = "/setPhone",method = RequestMethod.POST)
    public String getPhone(@RequestParam("phone") String phone, HttpSession httpSession){
     try{
        SendSms sendSms = new SendSms();
        RandomSMS randomSMS = new RandomSMS();
        int verifyCode = randomSMS.sendsms();
        sendSms.SMSverifyCode(phone, verifyCode);
        httpSession.setAttribute("verifyCode", verifyCode);
        httpSession.setAttribute("verifyCodeCreateTime", System.currentTimeMillis());
         return "home/Pwd3";
         }
     catch (Exception e){

         e.printStackTrace();
     }
        return "home/Pwd1";
    }
    @RequestMapping(value = "/SmsVerifyCode",method = RequestMethod.POST)
   @ResponseBody
    public int  SmsVerifyCode(Model model, Integer verifyCode,HttpSession httpSession){

        Integer verifyCode1=(Integer) httpSession.getAttribute("verifyCode");
       long verifyCodeCreateTime= System.currentTimeMillis()-Long.valueOf(String.valueOf(httpSession.getAttribute("verifyCodeCreateTime")));
             try {

                 if (verifyCodeCreateTime <= 1000 * 60 && verifyCode.equals(verifyCode1)) {
                     httpSession.removeAttribute("verifyCode");
                     httpSession.removeAttribute("verifyCodeCreateTime");
                   Integer id=(Integer) httpSession.getAttribute("id_password");
                   model.addAttribute("id",id);
                    // return "home/Pwd4";
                      return 0;
                 }
                 //超时
                 else  if(verifyCodeCreateTime> 1000 * 60){
                     httpSession.removeAttribute("verifyCode");
                     httpSession.removeAttribute("verifyCodeCreateTime");
                   //  return "";
                     return 1;
                 }
                 //验证码错误
                 else {
                     httpSession.removeAttribute("verifyCode");
                     httpSession.removeAttribute("verifyCodeCreateTime");
                     //return "home/Pwd1";
                     return 2;
                 }
                 //服务器出现故障
             }catch (Exception e) {
                 e.printStackTrace();
               //  return "home/Pwd1";
                 return 3;
             }

    }
   @RequestMapping("/inUpdataPassword")
   public  String  inUpdataPassword(HttpSession httpSession,Model model){
    Integer id=(Integer)  httpSession.getAttribute("id_password");
       model.addAttribute("id",id);
        return "home/Pwd4";
   }
   @RequestMapping("/pwd5")
   public String pwd5(){
        return "home/Pwd5";
   }
    @RequestMapping("/findAllLog")
    public  String findList(Model model){
       List<Login> list= iLoginService.findAll();
       model.addAttribute("list",list);
       return "admin/log";
    }
    @RequestMapping("/findLogByname")
    public  String findLogByname(String name,Model model){
      List<Login> list= iLoginService.byName(name);
      model.addAttribute("list",list);
      return "admin/log";
    }


}
